home *** CD-ROM | disk | FTP | other *** search
/ Aminet 4 / Aminet 4 - November 1994.iso / aminet / dev / obero / oberon_lib.lha / oberon-a / source1.lha / source / Amiga / IFFParse.mod < prev    next >
Text File  |  1994-08-08  |  17KB  |  599 lines

  1. (**************************************************************************
  2.  
  3.      $RCSfile: IFFParse.mod $
  4.   Description: Interface to iffparse.library
  5.  
  6.    Created by: fjc (Frank Copeland)
  7.     $Revision: 3.2 $
  8.       $Author: fjc $
  9.         $Date: 1994/08/08 00:56:10 $
  10.  
  11.   Includes Release 40.15
  12.  
  13.   (C) Copyright 1985-1993 Commodore-Amiga, Inc.
  14.       All Rights Reserved
  15.  
  16.   Oberon-A interface Copyright © 1994, Frank Copeland.
  17.   This file is part of the Oberon-A Interface.
  18.   See Oberon-A.doc for conditions of use and distribution.
  19.  
  20. ***************************************************************************)
  21.  
  22. MODULE IFFParse;
  23.  
  24. (*
  25. ** $C- CaseChk       $I- IndexChk  $L+ LongAdr   $N- NilChk
  26. ** $P- PortableCode  $R- RangeChk  $S- StackChk  $T- TypeChk
  27. ** $V- OvflChk       $Z- ZeroVars
  28. *)
  29.  
  30. IMPORT E := Exec, C := Clipboard, U := Utility, SYS := SYSTEM;
  31.  
  32. (*
  33. **      $VER: iffparse.h 39.1 (1.6.92)
  34. **
  35. **      iffparse.library structures and constants
  36. *)
  37.  
  38.  
  39. TYPE
  40.  
  41. (* Structure associated with an active IFF stream.
  42.  * "stream" is a value used by the client's read/write/seek functions -
  43.  * it will not be accessed by the library itself and can have any value
  44.  * (could even be a pointer or a BPTR).
  45.  *
  46.  * This structure can only be allocated by iffparse.library
  47.  *)
  48.   IFFHandlePtr * = CPOINTER TO IFFHandle;
  49.   IFFHandle * = RECORD
  50.     stream * : E.ULONG;
  51.     flags *  : SET;
  52.     depth *  : LONGINT;      (*  Depth of context stack.  *)
  53.   END; (* IFFHandle *)
  54.  
  55. (*
  56.  * Bit masks for "IFFHandle.flags" field.
  57.  *)
  58.  
  59. CONST
  60.  
  61.   read *       = {};                     (* read mode - default *)
  62.   write *      = {0};                    (* write mode *)
  63.   rwBits *     = read + write;           (* read/write bits *)
  64.   fSeek *      = {1};                    (* forward seek only *)
  65.   rSeek *      = {2};                    (* random seek *)
  66.   reserved *   = {16 .. 31};             (* Don't touch these bits. *)
  67.  
  68. (*****************************************************************************)
  69.  
  70. (*
  71.  * When the library calls your stream handler, you'll be passed a pointer
  72.  * to this structure as the "message packet".
  73.  *)
  74.  
  75. TYPE
  76.  
  77.   IFFStreamCmdPtr * = CPOINTER TO IFFStreamCmd;
  78.   IFFStreamCmd * = RECORD
  79.     command * : LONGINT;     (*  Operation to be performed (cmd...) *)
  80.     buf *     : E.APTR;      (*  Pointer to data buffer              *)
  81.     nBytes *  : LONGINT;     (*  Number of bytes to be affected      *)
  82.   END; (* IFFStreamCmd *)
  83.  
  84. (*****************************************************************************)
  85.  
  86. (*
  87.  * A node associated with a context on the iffStack.  Each node
  88.  * represents a chunk, the stack representing the current nesting
  89.  * of chunks in the open IFF file.  Each context node has associated
  90.  * local context items in the (private) LocalItems list.  The ID, type,
  91.  * size and scan values describe the chunk associated with this node.
  92.  *
  93.  * This structure can only be allocated by iffparse.library
  94.  *)
  95.  
  96. TYPE
  97.  
  98.   ContextNodePtr * = CPOINTER TO ContextNode;
  99.   ContextNode * = RECORD (E.MinNode)
  100.     id *   : LONGINT;
  101.     type * : LONGINT;
  102.     size * : LONGINT;        (*  Size of this chunk             *)
  103.     scan * : LONGINT;        (*  # of bytes read/written so far *)
  104.   END; (* ContextNode *)
  105.  
  106. (*****************************************************************************)
  107.  
  108. (*
  109.  * Local context items live in the ContextNode's.  Each class is identified
  110.  * by its lciIdent code and has a (private) purge vector for when the
  111.  * parent context node is popped.
  112.  *
  113.  * This structure can only be allocated by iffparse.library
  114.  *)
  115.  
  116. TYPE
  117.  
  118.   LocalContextItemPtr * = CPOINTER TO LocalContextItem;
  119.   LocalContextItem * = RECORD (E.MinNode)
  120.     id *    : E.ULONG;
  121.     type *  : E.ULONG;
  122.     ident * : E.ULONG;
  123.   END; (* LocalContextItem *)
  124.  
  125. (*****************************************************************************)
  126.  
  127. (*
  128.  * StoredProperty: a local context item containing the data stored
  129.  * from a previously encountered property chunk.
  130.  *)
  131.  
  132. TYPE
  133.  
  134.   StoredPropertyPtr * = CPOINTER TO StoredProperty;
  135.   StoredProperty * = RECORD
  136.     size * : LONGINT;
  137.     data * : E.APTR;
  138.   END; (* StoredProperty *)
  139.  
  140. (*****************************************************************************)
  141.  
  142. (*
  143.  * Collection Item: the actual node in the collection list at which
  144.  * client will look.  The next pointers cross context boundaries so
  145.  * that the complete list is accessable.
  146.  *)
  147.  
  148. TYPE
  149.  
  150.   CollectionItemPtr * = CPOINTER TO CollectionItem;
  151.   CollectionItem * = RECORD
  152.     next * : CollectionItemPtr;
  153.     size * : LONGINT;
  154.     data * : E.APTR;
  155.   END; (* CollectionItem *)
  156.  
  157. (*****************************************************************************)
  158.  
  159. (*
  160.  * Structure returned by OpenClipboard().  You may do cmdPosts and such
  161.  * using this structure.  However, once you call OpenIFF(), you may not
  162.  * do any more of your own I/O to the clipboard until you call CloseIFF().
  163.  *)
  164.  
  165. TYPE
  166.  
  167.   ClipboardHandlePtr * = CPOINTER TO ClipboardHandle;
  168.   ClipboardHandle * = RECORD (C.IOClipReq)
  169.     cbport *      : E.MsgPort;
  170.     satisfyPort * : E.MsgPort;
  171.   END; (* ClipboardHandle *)
  172.  
  173. (*****************************************************************************)
  174.  
  175. (*
  176.  * IFF return codes.  Most functions return either zero for success or
  177.  * one of these codes.  The exceptions are the read/write functions which
  178.  * return positive values for number of bytes or records read or written,
  179.  * or a negative error code.  Some of these codes are not errors per sae,
  180.  * but valid conditions such as EOF or EOC (End of Chunk).
  181.  *)
  182.  
  183. CONST
  184.  
  185.   errEOF *              = -1;     (*  Reached logical end of file *)
  186.   errEOC *              = -2;     (*  About to leave context      *)
  187.   errNoScope *          = -3;     (*  No valid scope for property *)
  188.   errNoMem *            = -4;     (*  Internal memory alloc failed*)
  189.   errRead *             = -5;     (*  Stream read error           *)
  190.   errWrite *            = -6;     (*  Stream write error          *)
  191.   errSeek *             = -7;     (*  Stream seek error           *)
  192.   errMangled *          = -8;     (*  Data in file is corrupt     *)
  193.   errSyntax *           = -9;     (*  IFF syntax error            *)
  194.   errNotIFF *           = -10;    (*  Not an IFF file             *)
  195.   errNoHook *           = -11;    (*  No call-back hook provided  *)
  196.   return2Client *       = -12;    (*  Client handler normal return*)
  197.  
  198. (*****************************************************************************)
  199.  
  200. (*
  201.  * Universal IFF identifiers.
  202.  *)
  203.  
  204. CONST
  205.  
  206.   idForm *   = 0464F524DH;  (* MakeID('F','O','R','M') *)
  207.   idList *   = 04C495354H;  (* MakeID('L','I','S','T') *)
  208.   idCat *    = 043415420H;  (* MakeID('C','A','T',' ') *)
  209.   idProp *   = 050524F50H;  (* MakeID('P','R','O','P') *)
  210.   idNull *   = 020202020H;  (* MakeID(' ',' ',' ',' ') *)
  211.  
  212. (*
  213.  * Identifier codes for universally recognized local context items.
  214.  *)
  215.  
  216. CONST
  217.  
  218.   lciProp *         = 070726F70H;  (* MakeID('p','r','o','p') *)
  219.   lciCollection *   = 0636F6C6CH;  (* MakeID('c','o','l','l') *)
  220.   lciEntryHandler * = 0656E6864H;  (* MakeID('e','n','h','d') *)
  221.   lciExitHandler *  = 065786864H;  (* MakeID('e','x','h','d') *)
  222.  
  223. (*****************************************************************************)
  224.  
  225. (*
  226.  * Control modes for ParseIFF() function.
  227.  *)
  228.  
  229. CONST
  230.  
  231.   parseScan *           = 0;
  232.   parseStep *           = 1;
  233.   parseRawStep *        = 2;
  234.  
  235. (*****************************************************************************)
  236.  
  237. (*
  238.  * Control modes for StoreLocalItem().
  239.  *)
  240.  
  241. CONST
  242.  
  243.   sliRoot *             = 1;      (*  Store in default context       *)
  244.   sliTop *              = 2;      (*  Store in current context       *)
  245.   sliProp *             = 3;      (*  Store in topmost FORM or LIST  *)
  246.  
  247. (*****************************************************************************)
  248.  
  249. (* Magic value for writing functions. If you pass this value in as a size
  250.  * to PushChunk() when writing a file, the parser will figure out the
  251.  * size of the chunk for you. If you know the size, is it better to
  252.  * provide as it makes things faster.
  253.  *)
  254.  
  255.   sizeUnknown *         = -1;
  256.  
  257. (*****************************************************************************)
  258.  
  259. (*
  260.  * Possible call-back command values.  (Using 0 as the value for iffCmdInit
  261.  * was, in retrospect, probably a bad idea.)
  262.  *)
  263.  
  264. CONST
  265.  
  266.   cmdInit *     = 0;       (*  Prepare the stream for a session    *)
  267.   cmdCleanup *  = 1;       (*  Terminate stream session            *)
  268.   cmdRead *     = 2;       (*  Read bytes from stream              *)
  269.   cmdWrite *    = 3;       (*  Write bytes to stream               *)
  270.   cmdSeek *     = 4;       (*  Seek on stream                      *)
  271.   cmdEntry *    = 5;       (*  You just entered a new context      *)
  272.   cmdExit *     = 6;       (*  You're about to leave a context     *)
  273.   cmdPurgeLCI * = 7;       (*  Purge a LocalContextItem            *)
  274.  
  275. (*****************************************************************************)
  276.  
  277. (* Obsolete IFFParse definitions, here for source code compatibility only.
  278.  * Please do NOT use in new code.
  279.  *)
  280. CONST
  281.  
  282.   sccInit *     = cmdInit;
  283.   sccCleanup *  = cmdCleanup;
  284.   sccRead *     = cmdRead;
  285.   sccWrite *    = cmdWrite;
  286.   sccSeek *     = cmdSeek;
  287.  
  288.  
  289. (*-- Library Base variable --------------------------------------------*)
  290.  
  291. TYPE
  292.  
  293.   IffParseBasePtr * = CPOINTER TO IffParseBase;
  294.   IffParseBase * = RECORD (E.Library) END;
  295.  
  296. CONST
  297.  
  298.   Name * = "iffparse.library";
  299.   iffparseName * = Name;
  300.  
  301. VAR
  302.  
  303.   base * : IffParseBasePtr;
  304.  
  305.  
  306. (*-- Library Functions ------------------------------------------------*)
  307.  
  308. (*
  309. **      $VER: iffparse_protos.h 39.1 (1.6.92)
  310. *)
  311.  
  312. (*--- functions in V36 or higher (Release 2.0) ---*)
  313.  
  314. (* ------ Basic functions ------*)
  315.  
  316. LIBCALL (base : IffParseBasePtr) AllocIFF* ()
  317.   : IFFHandlePtr;
  318.   -30;
  319. LIBCALL (base : IffParseBasePtr) OpenIFF*
  320.   ( iff    [8] : IFFHandlePtr;
  321.     rwMode [0] : SET )
  322.   : LONGINT;
  323.   -36;
  324. LIBCALL (base : IffParseBasePtr) ParseIFF*
  325.   ( iff     [8] : IFFHandlePtr;
  326.     control [0] : LONGINT )
  327.   : LONGINT;
  328.   -42;
  329. LIBCALL (base : IffParseBasePtr) CloseIFF*
  330.   ( iff [8] : IFFHandlePtr );
  331.   -48;
  332. LIBCALL (base : IffParseBasePtr) FreeIFF*
  333.   ( iff [8] : IFFHandlePtr );
  334.   -54;
  335.  
  336. (* ------ Read/Write functions ------*)
  337.  
  338. LIBCALL (base : IffParseBasePtr) ReadChunkBytes*
  339.   ( iff      [8] : IFFHandlePtr;
  340.     VAR buf  [9] : ARRAY OF SYS.BYTE;
  341.     size     [0] : LONGINT )
  342.   : LONGINT;
  343.   -60;
  344. LIBCALL (base : IffParseBasePtr) WriteChunkBytes*
  345.   ( iff  [8] : IFFHandlePtr;
  346.     buf  [9] : ARRAY OF SYS.BYTE;
  347.     size [0] : LONGINT )
  348.   : LONGINT;
  349.   -66;
  350. LIBCALL (base : IffParseBasePtr) ReadChunkRecords*
  351.   ( iff            [8] : IFFHandlePtr;
  352.     VAR buf        [9] : ARRAY OF SYS.BYTE;
  353.     bytesPerRecord [0] : LONGINT;
  354.     nRecords       [1] : LONGINT )
  355.   : LONGINT;
  356.   -72;
  357. LIBCALL (base : IffParseBasePtr) WriteChunkRecords*
  358.   ( iff            [8] : IFFHandlePtr;
  359.     buf            [9] : ARRAY OF SYS.BYTE;
  360.     bytesPerRecord [0] : LONGINT;
  361.     nRecords       [1] : LONGINT )
  362.   : LONGINT;
  363.   -78;
  364.  
  365. (* ------ Context entry/exit ------*)
  366.  
  367. LIBCALL (base : IffParseBasePtr) PushChunk*
  368.   ( iff  [8] : IFFHandlePtr;
  369.     type [0] : LONGINT;
  370.     id   [1] : LONGINT;
  371.     size [2] : LONGINT )
  372.   : LONGINT;
  373.   -84;
  374. LIBCALL (base : IffParseBasePtr) PopChunk*
  375.   ( iff [8] : IFFHandlePtr )
  376.   : LONGINT;
  377.   -90;
  378.  
  379. (* ------ Low-level handler installation ------*)
  380.  
  381. LIBCALL (base : IffParseBasePtr) EntryHandler*
  382.   ( iff      [8] : IFFHandlePtr;
  383.     type     [0] : LONGINT;
  384.     id       [1] : LONGINT;
  385.     position [2] : LONGINT;
  386.     handler  [9] : U.HookPtr;
  387.     object  [10] : E.APTR )
  388.   : LONGINT;
  389.   -102;
  390. LIBCALL (base : IffParseBasePtr) ExitHandler*
  391.   ( iff      [8] : IFFHandlePtr;
  392.     type     [0] : LONGINT;
  393.     id       [1] : LONGINT;
  394.     position [2] : LONGINT;
  395.     handler  [9] : U.HookPtr;
  396.     object  [10] : E.APTR )
  397.   : LONGINT;
  398.   -108;
  399.  
  400. (* ------ Built-in chunk/property handlers ------*)
  401.  
  402. LIBCALL (base : IffParseBasePtr) PropChunk*
  403.   ( iff  [8] : IFFHandlePtr;
  404.     type [0] : LONGINT;
  405.     id   [1] : LONGINT )
  406.   : LONGINT;
  407.   -114;
  408. LIBCALL (base : IffParseBasePtr) PropChunks*
  409.   ( iff       [8] : IFFHandlePtr;
  410.     propArray [9] : ARRAY OF LONGINT;
  411.     nProps    [0] : LONGINT )
  412.   : LONGINT;
  413.   -120;
  414. LIBCALL (base : IffParseBasePtr) StopChunk*
  415.   ( iff  [8] : IFFHandlePtr;
  416.     type [0] : LONGINT;
  417.     id   [1] : LONGINT )
  418.   : LONGINT;
  419.   -126;
  420. LIBCALL (base : IffParseBasePtr) StopChunks*
  421.   ( iff       [8] : IFFHandlePtr;
  422.     propArray [9] : ARRAY OF LONGINT;
  423.     nProps    [0] : LONGINT )
  424.   : LONGINT;
  425.   -132;
  426. LIBCALL (base : IffParseBasePtr) CollectionChunk*
  427.   ( iff  [8] : IFFHandlePtr;
  428.     type [0] : LONGINT;
  429.     id   [1] : LONGINT )
  430.   : LONGINT;
  431.   -138;
  432. LIBCALL (base : IffParseBasePtr) CollectionChunks*
  433.   ( iff       [8] : IFFHandlePtr;
  434.     propArray [9] : ARRAY OF LONGINT;
  435.     nProps    [0] : LONGINT )
  436.   : LONGINT;
  437.   -144;
  438. LIBCALL (base : IffParseBasePtr) StopOnExit*
  439.   ( iff  [8] : IFFHandlePtr;
  440.     type [0] : LONGINT;
  441.     id   [1] : LONGINT )
  442.   : LONGINT;
  443.   -150;
  444.  
  445. (* ------ Context utilities ------*)
  446.  
  447. LIBCALL (base : IffParseBasePtr) FindProp*
  448.   ( iff  [8] : IFFHandlePtr;
  449.     type [0] : LONGINT;
  450.     id   [1] : LONGINT )
  451.   : StoredPropertyPtr;
  452.   -156;
  453. LIBCALL (base : IffParseBasePtr) FindCollection*
  454.   ( iff  [8] : IFFHandlePtr;
  455.     type [0] : LONGINT;
  456.     id   [1] : LONGINT )
  457.   : CollectionItemPtr;
  458.   -162;
  459. LIBCALL (base : IffParseBasePtr) FindPropContext*
  460.   ( iff [8] : IFFHandlePtr )
  461.   : ContextNodePtr;
  462.   -168;
  463. LIBCALL (base : IffParseBasePtr) CurrentChunk*
  464.   ( iff [8] : IFFHandlePtr )
  465.   : ContextNodePtr;
  466.   -174;
  467. LIBCALL (base : IffParseBasePtr) ParentChunk*
  468.   ( contextNode [8] : ContextNodePtr )
  469.   : ContextNodePtr;
  470.   -180;
  471.  
  472. (* ------ LocalContextItem support functions ------*)
  473.  
  474. LIBCALL (base : IffParseBasePtr) AllocLocalItem*
  475.   ( type     [0] : LONGINT;
  476.     id       [1] : LONGINT;
  477.     ident    [2] : LONGINT;
  478.     dataSize [3] : LONGINT )
  479.   : LocalContextItemPtr;
  480.   -186;
  481. LIBCALL (base : IffParseBasePtr) LocalItemData*
  482.   ( localItem [8] : LocalContextItemPtr )
  483.   : E.APTR;
  484.   -192;
  485. LIBCALL (base : IffParseBasePtr) SetLocalItemPurge*
  486.   ( localItem [8] : LocalContextItemPtr;
  487.     purgeHook [9] : U.HookPtr );
  488.   -198;
  489. LIBCALL (base : IffParseBasePtr) FreeLocalItem*
  490.   ( localItem [8] : LocalContextItemPtr );
  491.   -204;
  492. LIBCALL (base : IffParseBasePtr) FindLocalItem*
  493.   ( iff   [8] : IFFHandlePtr;
  494.     type  [0] : LONGINT;
  495.     id    [1] : LONGINT;
  496.     ident [2] : LONGINT )
  497.   : LocalContextItemPtr;
  498.   -210;
  499. LIBCALL (base : IffParseBasePtr) StoreLocalItem*
  500.   ( iff       [8] : IFFHandlePtr;
  501.     localItem [9] : LocalContextItemPtr;
  502.     position  [0] : LONGINT )
  503.   : LONGINT;
  504.   -216;
  505. LIBCALL (base : IffParseBasePtr) StoreItemInContext*
  506.   ( iff          [8] : IFFHandlePtr;
  507.     localItem    [9] : LocalContextItemPtr;
  508.     contextNode [10] : ContextNodePtr );
  509.   -222;
  510.  
  511. (* ------ IFFHandle initialization ------*)
  512.  
  513. LIBCALL (base : IffParseBasePtr) InitIFF*
  514.   ( iff        [8] : IFFHandlePtr;
  515.     flags      [0] : SET;
  516.     streamHook [9] : U.HookPtr );
  517.   -228;
  518. LIBCALL (base : IffParseBasePtr) InitIFFasDOS*
  519.   ( iff [8] : IFFHandlePtr );
  520.   -234;
  521. LIBCALL (base : IffParseBasePtr) InitIFFasClip*
  522.   ( iff [8] : IFFHandlePtr );
  523.   -240;
  524.  
  525. (* ------ Internal clipboard support ------*)
  526.  
  527. LIBCALL (base : IffParseBasePtr) OpenClipboard*
  528.   ( unitNum [0] : LONGINT )
  529.   : ClipboardHandlePtr;
  530.   -246;
  531. LIBCALL (base : IffParseBasePtr) CloseClipboard*
  532.   ( clipboard [8] : ClipboardHandlePtr );
  533.   -252;
  534.  
  535. (* ------ Miscellaneous ------*)
  536.  
  537. LIBCALL (base : IffParseBasePtr) GoodID*
  538.   ( id [0] : LONGINT )
  539.   : LONGINT;
  540.   -258;
  541. LIBCALL (base : IffParseBasePtr) GoodType*
  542.   ( type [0] : LONGINT )
  543.   : LONGINT;
  544.   -264;
  545. LIBCALL (base : IffParseBasePtr) IDtoStr*
  546.   ( id      [0] : LONGINT;
  547.     VAR buf [8] : ARRAY OF CHAR )
  548.   : E.STRPTR;
  549.   -270;
  550.  
  551.  
  552. (*-- C Macros defined as procedures -----------------------------------*)
  553. (* $L+ Absolute long addressing for globals *)
  554.  
  555.  
  556. (*-----------------------------------*)
  557. (*$D-*)
  558. PROCEDURE MakeID (id : ARRAY OF CHAR) : LONGINT;
  559. BEGIN (* MakeID *)
  560.   RETURN
  561.     ( SYS.LSH (LONG (ORD (id [0])), 24)
  562.       + SYS.LSH (LONG (ORD (id [1])), 16)
  563.       + SYS.LSH (LONG (ORD (id [2])), 8)
  564.       + LONG (ORD (id [3])) )
  565. END MakeID;
  566. (*$D+*)
  567.  
  568.  
  569. (*-- Library Base variable --------------------------------------------*)
  570. (* $L- Address globals through A4 *)
  571.  
  572.  
  573. (*-----------------------------------*)
  574. PROCEDURE* CloseLib ();
  575.  
  576. BEGIN (* CloseLib *)
  577.   IF base # NIL THEN E.base.CloseLibrary (base) END
  578. END CloseLib;
  579.  
  580. (*-----------------------------------*)
  581. PROCEDURE OpenLib * (mustOpen : BOOLEAN);
  582.  
  583. BEGIN (* OpenLib *)
  584.   IF base = NIL THEN
  585.     base :=
  586.       SYS.VAL
  587.         ( IffParseBasePtr,
  588.           E.base.OpenLibrary (Name, E.libraryMinimum) );
  589.     IF base # NIL THEN SYS.SETCLEANUP (CloseLib)
  590.     ELSIF mustOpen THEN HALT (100)
  591.     END;
  592.   END;
  593. END OpenLib;
  594.  
  595.  
  596. BEGIN
  597.   base := NIL
  598. END IFFParse.
  599.